package droid.frame.utils.android;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.NotificationCompat;

import droid.frame.App;

/**
 * 通知工具栏
 * 
 * @author coffee<br>
 *         2013-1-24上午9:54:42
 */
public class Notify {

	private Notify() {
	}

	private static Notify instance;

	public static Notify getInstance() {
		if (instance == null) {
			instance = new Notify();
		}
		return instance;
	}

	private NotificationManager mNotificationManager;
	private NotificationCompat.Builder builder = null;

	/**
	 * 
	 * [显示通知]<BR>
	 * 用synchronized保持该方法实例化的对象 builder跟{@link Notify}中的builder对象一致
	 * 
	 * @param intent
	 *            ： 点击通知触发的Intent
	 * @param tickerText
	 *            ： 通知顶部文字
	 * @param contentTitle
	 *            ： 通知标题
	 * @param contentText
	 *            : 通知详情文字
	 */
	public synchronized Notify build(Intent intent, String tickerText, String contentTitle, String contentText) {
		if (isValid()) {
			Context context = App.getContext();
			if (intent == null) {
				intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
			}
			builder = new NotificationCompat.Builder(context);
			int icon = context.getResources().getIdentifier("ic_launcher", "drawable", context.getPackageName());
			int requestID = (int) System.currentTimeMillis();
			// intent.setAction("android.settings.SETTINGS");
			PendingIntent pendingIntent = PendingIntent.getActivity(context, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

			// 添加通知样式
			NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
			bigTextStyle.setBigContentTitle(contentTitle);
			bigTextStyle.bigText(contentText);
			//
			builder.setContentIntent(pendingIntent) //
					.setSmallIcon(icon)//
					.setLargeIcon(null)//
					.setTicker(tickerText)//
					.setOnlyAlertOnce(false) //
					.setWhen(System.currentTimeMillis())//
					.setContentTitle(contentTitle)//
					.setOngoing(false) // 不能cancel
					.setAutoCancel(true) // 设置自动取消
					.setStyle(bigTextStyle).setContentText(contentText);//
		}
		return this;
	}

	public void notify(int notifyId) {
		if (builder != null) {
			Notification notification = builder.build();
			// 先取消。如果发通知之前。该通知已经存在,那么tickerText将不会在此显示
			// mNotificationManager.cancel(notifyId);
			mNotificationManager.notify(notifyId, notification);
		}
	}

	/**
	 * 
	 * @param notifyId
	 * @param cancelable
	 *            Notification 是否可以取消
	 */
	public void notify(int notifyId, boolean cancelable) {
		if (cancelable) {
			builder.setOngoing(true);// 不能cancel
		}
		this.notify(notifyId);
	}

	/**
	 * 提高Service的优先级<br>
	 * 发一个Notification
	 */
	public void startForeground(Service service) {
		int appName = service.getResources().getIdentifier("app_name", "string", service.getPackageName());
		String appNameStr = service.getResources().getString(appName);
		PackageManager pm = service.getPackageManager();
		Intent intent = pm.getLaunchIntentForPackage(service.getPackageName());
		// 启动主类。效果跟单击桌面launcher的效果一样
		Intent startIntent = new Intent();
		startIntent.setClassName(service, intent.getComponent().getClassName());
		startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startIntent.setAction(Intent.ACTION_MAIN);
		startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		// service.startActivity(startIntent);
		build(startIntent, appNameStr, appNameStr, "请保持程序在后台运行");
		Notification notification = builder.build();
		service.startForeground(0x111, notification);
	}

	/**
	 * 
	 * [取消通知]<BR>
	 * [功能详细描述]
	 * 
	 * @param id
	 *            该通知的id
	 */
	public void cancel(int id) {
		if (isValid()) {
			mNotificationManager.cancel(id);
		}
	}

	/**
	 * 
	 * [通知栏是否可用]<BR>
	 * [功能详细描述]
	 * 
	 * @return
	 */
	private boolean isValid() {
		Context context = App.getContext();
		if (mNotificationManager == null) {
			mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		}
		return mNotificationManager != null;
	}
}
